function getScrollingPosition() {
	var position = [0, 0];
	
	if (typeof window.pageYOffset != 'undefined') {
		position = [window.pageXOffset, window.pageYOffset];
	} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0 || document.documentElement.scollLeft > 0) {
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	} else if (typeof document.body.scrollTop != 'undefined') {
		position = [document.body.scrollLeft, document.body.scrollTop];
	}
	
	return position;
}

function confirmMessage(message) {
	//var span = document.getElementById('msg');
	var div = document.getElementById('confirmmsg');
	if (div) {
		div.innerHTML = '<img src="img/success.png" />'+message;
		div.style.display = 'block';
	}
	
}

function exportAs(link) {
	var div = document.getElementById('exportas');
	var button = document.getElementById(link.id);
	var pos = getPosition(button);
	var buttonposX = pos[0];
	var buttonposY = pos[1];

	var newDivPosX = buttonposX;
	var newDivPosY = buttonposY + (5 + button.clientHeight);

	div.style.top = newDivPosY;
	div.style.left = newDivPosX;
	div.style.display = 'block';

	//window.setTimeout('hideExportAs()', 5000);

	return false;
}

function hideExportTimeout() {
	window.setTimeout('hideTimeout()', 5000);
}

function cancelExportTimeout() {
	clearTimeout(window.setTimeout('hideTimeout()', 5000));
}

function hideTimeout() {
	var div = document.getElementById('exportas');
	if (div.style.display != 'none') {
		div.style.display = 'none';
	}
}

function getPosition(theElement) {
	var posX = 0;
	var posY = 0;
	
	while (theElement) {
		posX += theElement.offsetLeft;
		posY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	
	return [posX, posY];
}

function confirmSave(link) {
	var frm = link.form;
	var ok = confirm("The invoice needs to be saved before you can export. \n Do you want to save now?");
	if (ok) {
		frm.submit;
	} else {
		return false;
	}
}

function submitDeleteForm(id) {
	if (id) {
		var frm = document.getElementById('delform');
		if (frm) {
			var input = document.getElementById('delform_id');
			if (input) {
				input.value = id;
				frm.submit();
			}
		}
	} 
}

function setValue(content,element) {
	var field = document.getElementById(element);
	if (field) {
		field.value = content;
	}
}

function cloneRow(event) {
	cloneInvoiceRow(event.target);
}

function cloneInvoiceRow(link) {
	for (var origtr = link; origtr.nodeName != 'TR'; origtr = origtr.parentNode);

	var newid = origtr.id;
	newid++;

	var itemoptions = document.getElementById('item_itemid[New][0]');

	if (origtr.getAttribute('cloned') != 1) {
		origtr.setAttribute('cloned', 1);
	
		for (var prnt = link; prnt.nodeName != 'TBODY'; prnt = prnt.parentNode);
		if (prnt == link) {
			for (var prnt = link; prnt.nodeName != 'TABLE'; prnt = prnt.parentNode);
		}

		var tr = document.createElement('tr');
		prnt.insertBefore(tr, prnt.lastChild.nextSibling);
		tr.setAttribute('id', newid);

		var td0 = document.createElement('td');
		var td1 = document.createElement('td');
		var td2 = document.createElement('td');
		var td3 = document.createElement('td');
		var td4 = document.createElement('td');
		var td5 = document.createElement('td');

		var iteminput = createNamedElement('select', 'item_itemid[New]['+newid+']');
		iteminput.id = 'item_itemid[New]['+newid+']';
		iteminput.onchange=Function('cloneInvoiceRow(this)');

		for (var i=0; i < itemoptions.length; i++) {
			var option = document.createElement('option');
			option.text = itemoptions[i].text;
			option.value = itemoptions[i].value;
			iteminput.appendChild(option);
		}

		var itemdescinput = createNamedElement('input', 'item_description[New]['+newid+']');
		itemdescinput.setAttribute('type', 'text');
		itemdescinput.setAttribute('id', 'item_description[New]['+newid+']');

		var qtyinput = createNamedElement('input', 'item_quantity[New]['+newid+']')
		qtyinput.setAttribute('type', 'text');
		qtyinput.setAttribute('id', 'item_quantity[New]['+newid+']');

		var amountinput = createNamedElement('input', 'item_amounteach[New]['+newid+']');
		amountinput.setAttribute('type', 'text');
		amountinput.setAttribute('id', 'item_amounteach[New]['+newid+']');

		var taxinput = createNamedElement('input', 'item_taxamount[New]['+newid+']');
		taxinput.setAttribute('type', 'text');
		taxinput.setAttribute('id', 'item_taxamount[New]['+newid+']');

		var totalinput = createNamedElement('input', 'rowtotal[New]['+newid+']');
		totalinput.setAttribute('type', 'text');
		totalinput.setAttribute('id', 'rowtotal[New]['+newid+']');

		td0.appendChild(iteminput);	
		td1.appendChild(itemdescinput);
		td2.appendChild(qtyinput);
		td3.appendChild(amountinput);
		td4.appendChild(taxinput);
		td5.appendChild(totalinput);

		tr.appendChild(td0);
		tr.appendChild(td1);		
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		tr.appendChild(td5);
	}
	attachEventListener(document.getElementById('item_itemid[New]['+newid+']'), 'change', populateInvoice, false);
	attachEventListener(document.getElementById('item_itemid[New]['+newid+']'), 'change', cloneRow, false);
	attachEventListener(document.getElementById('item_quantity[New]['+newid+']'), 'change', updateTotal, false);
}
